package liangtWorkSpace.simple;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RemoveDuplicates
 * @Description : 获取有序、不重复数列并返回长度
 * @Author : LiangT
 * @Date : 2020/6/16 11:45
 */
public class RemoveDuplicates {

    /**
     * 使用逻辑空间
     * */
    public int removeDuplicates(int[] nums) {

        if (nums == null || nums.length < 1){
            return 0;
        }
        List<Integer> list = new ArrayList();
        for (int i = 0; i < nums.length;i++){
            if (i == 0){
                list.add(nums[0]);
            }else {
                if (nums[i] == list.get(list.size()-1)){
                    continue;
                }
                list.add(nums[i]);
                nums[list.size()-1] = nums[i];
            }
        }
        return list.size();
    }

    public int removeDuplicates1(int[] nums) {

        if (nums == null || nums.length < 1) {
            return 0;
        }
        int num = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[num-1]) {
                continue;
            }
            nums[num] = nums[i];
            num++;
        }
        return num;
    }

    public static void main(String[] args) {
        RemoveDuplicates removeDuplicates = new RemoveDuplicates();
        int[] j = null;
        System.out.println(j.length);
        int[] i = {0,0,1,1,1,2,2,3,3,4};
        System.out.println(removeDuplicates.removeDuplicates1(i));
    }
}
